package org.rkuk.kasutil.media 
{
	/**
	 * ...
	 * @author rkuk
	 */
	internal class ResumeState extends SeekState 
	{
		private var isReseting:Boolean;
		private var isStopping:Boolean;
		private var isPlaying:Boolean;
		private var isPausing:Boolean;
		private function get isPlayingOrStopping():Boolean
		{
			return isPlaying || isStopping;
		}
		
		public function ResumeState(media:MediaProxy, isSeeking:Boolean)
		{
			super(media, isSeeking);
			isReseting = isStopping = isPlaying = isPausing = false;
			initStateListener();
		}
		
		/**
		 * 进入当前状态时，在没有操作的情况下就需要立即征听MediaProxy的stop和buffer事件
		 */
		private function initStateListener():void 
		{
			media.addEventListener(MediaStreamStatusEvent.STATUS_STOP, onMediaStop);
			media.addEventListener(MediaStreamStatusEvent.STATUS_BUFFER, onMediaBuffer);
		}
		
		/**
		 * @inheritDoc
		 */
		override public function get description():String 
		{
			return MediaSourceEvent.RESUME;
		}
		
		override public function dispose():void 
		{
			if (media.hasEventListener(MediaStreamStatusEvent.STATUS_PLAY))
				media.removeEventListener(MediaStreamStatusEvent.STATUS_PLAY, onMediaPlay);
			if (media.hasEventListener(MediaStreamStatusEvent.STATUS_BUFFER))
				media.removeEventListener(MediaStreamStatusEvent.STATUS_BUFFER, onMediaBuffer);
			if (media.hasEventListener(MediaStreamStatusEvent.STATUS_PAUSE))
				media.removeEventListener(MediaStreamStatusEvent.STATUS_PAUSE, onMediaPause);
			if (media.hasEventListener(MediaStreamStatusEvent.STATUS_STOP))
				media.removeEventListener(MediaStreamStatusEvent.STATUS_STOP, onMediaStop);
				
			super.dispose();
		}
		
		/**
		 * 征听MediaProxy发出的Buffer事件，切换状态并发出MediaSourceEvent
		 * @param	e	MediaProxy发出的Buffer事件
		 */
		private function onMediaBuffer(e:MediaStreamStatusEvent):void 
		{
			var media:MediaProxy = this.media;
			dispose();
			
			media.state = new BufferState(media, isSeeking);
		}
		
		/**
		 * 征听MediaProxy发出的Play事件，切换状态并发出MediaSourceEvent
		 * @param	e	MediaProxy发出的Play事件
		 */
		private function onMediaPlay(e:MediaStreamStatusEvent):void 
		{
			var media:MediaProxy = this.media;
			dispose();
			
			media.state = new PlayState(media);
		}
		
		/**
		 * 征听MediaProxy发出的暂停事件，切换状态并发出MediaSourceEvent
		 * @param	e	MediaProxy发出的暂停事件
		 */
		private function onMediaPause(e:MediaStreamStatusEvent):void 
		{
			var media:MediaProxy = this.media;
			dispose();
			
			media.state = new PauseState(media, isSeeking);
		}
		
		/**
		 * 征听MediaProxy发出的停止或播放完成事件，切换状态并发出MediaSourceEvent
		 * @param	e	MediaProxy发出的停止或播放完成事件
		 */
		private function onMediaStop(e:MediaStreamStatusEvent):void 
		{
			var media:MediaProxy = this.media;
			dispose();
			
			if (isStopping)
				media.state = new StopState(media);
			else
				media.state = new EndState(media);
				
			if (isReseting)
				media.state = new ResetState(media);
		}
		
		override public function start(...rest):void 
		{
			if (!isPlayingOrStopping)
			{
				if (media.hasEventListener(MediaStreamStatusEvent.STATUS_BUFFER))
					media.removeEventListener(MediaStreamStatusEvent.STATUS_BUFFER, onMediaBuffer);
				if (media.hasEventListener(MediaStreamStatusEvent.STATUS_SEEK))
					media.removeEventListener(MediaStreamStatusEvent.STATUS_SEEK, onMediaSeek);
				if (media.hasEventListener(MediaStreamStatusEvent.STATUS_PAUSE))
					media.removeEventListener(MediaStreamStatusEvent.STATUS_PAUSE, onMediaPause);
					
				isPlaying = true;
				media.addEventListener(MediaStreamStatusEvent.STATUS_PLAY, onMediaPlay);
				media.start.apply(media, rest);
			}
		}
		
		/**
		 * @inheritDoc
		 */
		override public function stop():void 
		{
			//if (!isStopping)
			if (!isPlayingOrStopping || (isReseting && isPlaying))
			{
				if (media.hasEventListener(MediaStreamStatusEvent.STATUS_PLAY))
					media.removeEventListener(MediaStreamStatusEvent.STATUS_PLAY, onMediaPlay);
				if (media.hasEventListener(MediaStreamStatusEvent.STATUS_BUFFER))
					media.removeEventListener(MediaStreamStatusEvent.STATUS_BUFFER, onMediaBuffer);
				if (media.hasEventListener(MediaStreamStatusEvent.STATUS_SEEK))
					media.removeEventListener(MediaStreamStatusEvent.STATUS_SEEK, onMediaSeek);
				if (media.hasEventListener(MediaStreamStatusEvent.STATUS_PAUSE))
					media.removeEventListener(MediaStreamStatusEvent.STATUS_PAUSE, onMediaPause);
					
				isStopping = true;
				//media.addEventListener(MediaStreamStatusEvent.STATUS_STOP, onMediaStop);
				media.stop();
			}
		}
		
		/**
		 * @inheritDoc
		 */
		override public function pause():void
		{
			if (!isPausing && !isPlayingOrStopping)
			{
				isPausing = true;
				media.addEventListener(MediaStreamStatusEvent.STATUS_PAUSE, onMediaPause);
				media.pause();
			}
		}
		
		/**
		 * @inheritDoc
		 */
		override public function seek(offset:Number):void 
		{
			if (!isPlayingOrStopping)
			{
				if (!isSeeking)
				{
					isSeeking = true;
					media.addEventListener(MediaStreamStatusEvent.STATUS_SEEK, onMediaSeek);
				}
				
				media.seek(offset);
			}
		}
		
		override public function reset():void 
		{
			if (!isReseting)
			{
				isReseting = true;
				stop();
			}
		}
	}
}